
eststo clear

if("`c(os)'"=="Windows"){
	local tableSaveDir = "Tables\"
	local plotSaveDir = "Plots\"
	local pythonSaveDir = "PythonScripts\"
}
else{
	local tableSaveDir = "Tables/"
	local plotSaveDir = "Plots/"
	local pythonSaveDir = "PythonScripts/"
}

//Establish Control Variable Sets
//Spec1 controls are the same for both registrations and fuel use 
local Spec1Controls = "c.normYear#i.stateGroup c.quadYear#i.stateGroup asinhMetroPop asinhNonMetroPop asinhMetroInc asinhNonMetroInc logrealmeangasprice logemployment loglicenseddrivers logrealstategdp logroadmileage"

//Other controls are not the same for both registrations and VMT 
//Registration Controls
local Spec2RegControls = "`Spec1Controls' i.transactionDataDummy"
local Spec3RegControls = "`Spec2RegControls' i.regDummyExtSpec3"
local Spec4RegControls = "`Spec2RegControls' i.regDummyExtSpec4"


di "`Spec3RegControls'"

//Gas use Controls
local Spec2GasUseControls = "`Spec1Controls'"
local Spec3GasUseControls = "`Spec2GasUseControls' i.gasUseDummyExtSpec3"
local Spec4GasUseControls = "`Spec2GasUseControls' i.gasUseDummyExtSpec4"

//Partialled-out variables for FWL
local partialVarsSpec1 = "_cons ib51.stateGroup ib1995.year c.normYear#i.stateGroup"

//Registration Partial Variables
local partialVarsSpec2 = "`partialVarsSpec1' i.transactionDataDummy"
local partialVarsSpec3 = "`partialVarsSpec2' i.regDummyExtSpec3"
local partialVarsSpec4 = "`partialVarsSpec2' i.regDummyExtSpec4"

//VMT Partial Variables
local partialVarsSpec2 = "`partialVarsSpec1'"
local partialVarsSpec3 = "`partialVarsSpec2' i.gasUseDummyExtSpec3"
local partialVarsSpec4 = "`partialVarsSpec2' i.gasUseDummyExtSpec4"

//Absorb Vars are always time and state fixed effects
local absorbVars = "ib51.stateGroup ib1995.year"

//Establish Cluster Variable
local clusterVar = "stateGroup year"

//Establish primary null
local primaryNull = -4.8
local altNull = 1
local altNull2 = 9.7
local altNull3 = -6.1
local altNull4 = 11

//Save as string due to round-off issues
local altNull2String = string(`altNull2', "%2.1f")
local altNull3String = string(`altNull3', "%2.1f")

//Define treatment Variable
local treatmentVar = "1.nosafetyind"

//Set up program arguments for the Spec2 model
local fsVarsSpec2 = "logregistrations i.nosafetyind `Spec2RegControls'"
local rfVarsSpec2 = "loghighwaygasuse i.nosafetyind `Spec2GasUseControls'"

local fsOut = "logregistrations"
local rfOut = "loghighwaygasuse"

local fsControls = "`Spec2RegControls'"
local rfControls = "`Spec2GasUseControls'"

local treatControl = "nosafetyind"

local treatmentVar = "1.nosafetyind"

local modelTitleSpec2 = "Spec2GasUse"
local nullLowSpec2 = -4
local nullDeltaSpec2 = 0.01
local nullHighSpec2 = 1

levelsof statecode
	local index = 2
	
	if("`c(os)'"=="Windows"){
		qui putexcel set "Tables\iv-loo.xlsx", sheet("LOO-Spec2GasUse", replace) modify
	}
	else{
		qui putexcel set "Tables/iv-loo.xlsx", sheet("LOO-Spec2GasUse", replace) modify
	}
	
	//Iterate through each state and remove relevant data to re-estimate model
	foreach tempSC in `r(levels)'{
		preserve
		
		qui putexcel A1 = "State Removed"
		qui putexcel B1 = "Pi"
		qui putexcel C1 = "Pi Variance"
		qui putexcel D1 = "Delta"
		qui putexcel E1 = "Delta Variance"
		qui putexcel F1 = "Pi-Delta Covariance"
		qui putexcel G1 = "AR(Beta=`primaryNull')"
		qui putexcel H1 = "P-Value(Gamma=`primaryNull')"
		qui putexcel I1 = "AR(Gamma=`altNull')"
		qui putexcel J1 = "P-Value(Gamma=`altNull')"
		qui putexcel K1 = "AR(Gamma=`altNull2String')"
		qui putexcel L1 = "P-Value(Gamma=`altNull2String')"
		qui putexcel M1 = "AR(Gamma=`altNull3String')"
		qui putexcel N1 = "P-Value(Gamma=`altNull3String')"
		qui putexcel O1 = "AR(Gamma=`altNull4')"
		qui putexcel P1 = "P-Value(Gamma=`altNull4')"
		qui putexcel Q1 = "Gamma"


		qui drop if statecode == "`tempSC'"
// 		jointEstimate `fsVarsSpec2', `rfVarsSpec2', `clusterVar', `treatmentVar', "`modelTitleSpec2' w/o `tempSC'"
		jointEstimateFWL `fsOut', `rfOut', `fsControls', `rfControls', `treatControl', `absorbVars', `clusterVar', `treatmentVar', "`modelTitleSpec2' w/o `tempSC'"
		local tempPi = `r(pi)'
		local tempDelta = `r(delta)'
		local tempPiVar = `r(piVar)'
		local tempDeltaVar = `r(deltaVar)'
		local tempCovar = `r(covar)'
		testNull `tempPi' `tempDelta' `tempPiVar' `tempDeltaVar' `tempCovar' `primaryNull'
		local tempPrimaryAR = `r(arStat)'
		local tempPrimaryPVal = `r(pVal)'
		local tempBeta = `r(beta)'
		testNull `tempPi' `tempDelta' `tempPiVar' `tempDeltaVar' `tempCovar' `altNull'
		local tempAltAR = `r(arStat)'
		local tempAltPVal = `r(pVal)'
		testNull `tempPi' `tempDelta' `tempPiVar' `tempDeltaVar' `tempCovar' `altNull2'
		local tempAltAR2 = `r(arStat)'
		local tempAltPVal2 = `r(pVal)'
		testNull `tempPi' `tempDelta' `tempPiVar' `tempDeltaVar' `tempCovar' `altNull3'
		local tempAltAR3 = `r(arStat)'
		local tempAltPVal3 = `r(pVal)'
		testNull `tempPi' `tempDelta' `tempPiVar' `tempDeltaVar' `tempCovar' `altNull4'
		local tempAltAR4 = `r(arStat)'
		local tempAltPVal4 = `r(pVal)'

		
		putexcel A`index' = "`tempSC'"
		qui putexcel B`index' = `tempPi'
		qui putexcel C`index' = `tempPiVar'
		qui putexcel D`index' = `tempDelta'
		qui putexcel E`index' = `tempDeltaVar'
		qui putexcel F`index' = `tempCovar'
		qui putexcel G`index' = `tempPrimaryAR'
		qui putexcel H`index' = `tempPrimaryPVal'
		qui putexcel I`index' = `tempAltAR'
		qui putexcel J`index' = `tempAltPVal'
		qui putexcel K`index' = `tempAltAR2'
		qui putexcel L`index' = `tempAltPVal2'
		qui putexcel M`index' = `tempAltAR3'
		qui putexcel N`index' = `tempAltPVal3'
		qui putexcel O`index' = `tempAltAR4'
		qui putexcel P`index' = `tempAltPVal4'
		qui putexcel Q`index' = `tempBeta'
		
		local index = `index'+1
		
		restore
	}	
